<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    const obj = {
      uname: 'ikun',
      hobby: ['sing', 'dance', 'rap'],
      age: '2.5',
      fans: {
        uname: '小黑子',
        active: '露出了鸡脚'
      }
    }
    // 浅拷贝
    let newObj = {}
    // 方法一：for in
    // for (let key in obj) {
    //   newObj[key] = obj[key]
    // }
    // 方法二：Object.assign
    Object.assign(newObj, obj)
    // 方法三：展开运算符
    newObj = { ...obj }

    // 深拷贝
    // 方法一：JSON方法(低阶：有不足)
    // newObj = JSON.parse(JSON.stringify(obj))
    // console.log(newObj)
    // console.log(obj)
    // 方法二：递归深拷贝
    // newObj = {}
    // function deepClone(newObj, oldObj) {
    //   for (let key in oldObj) {
    //     if (oldObj[key] instanceof Array) {
    //       newObj[key] = []
    //       deepClone(newObj[key], oldObj[key])
    //     }
    //     else if (oldObj[key] instanceof Object) {
    //       newObj[key] = {}
    //       deepClone(newObj[key], oldObj[key])
    //     }
    //     else {
    //       newObj[key] = oldObj[key]
    //     }
    //   }
    // }
    // deepClone(newObj, obj)
    // newObj.hobby[1] = '背带裤'
    console.log(newObj)
    console.log(obj);
  </script>
</body>

</html>